import { ref, computed, reactive } from 'vue'
import { defineStore } from 'pinia'
import { accountLoginApi, getUserInfoApi, getUserMenuTreeApi } from "@/api/login/login"

import { localCache } from "@/utils/cache"  // 引入 封装的本地缓存 工具
import { mapMenusToRoutes3 , loadLocalRouter ,mapMenusToFirstPath} from "@/utils/mapMenus"  // 引入 封装的 菜单映射工具

import { asyncRoutes } from "@/router/index"  // 获取router里 部分 定义好 准备 动态的路由
import  localRouter  from "@/router/localRouter"  // 获取提前定义好 和后端比较的 的路由


import router from "@/router";  

import type { RuleForm } from "@/types"
import type { RouteRecordRaw } from 'vue-router';
// 执行登录操作
export const useLoginStore = defineStore('useLoginStore', () => {
  // 获取token
  const token = ref<string>('')
  const userinfo = ref<any>({})
  const userMenus = ref<any>([])

  // 登录操作
  async function loginAccountAction(account: RuleForm) {
    const loginResult = await accountLoginApi(account)
    if (loginResult.code === 200) {
      // 1.登录成功 存储token
      token.value = loginResult.data.token
      localCache.setCache('token', loginResult.data.token)
      ElMessage.success('登录成功')
      let uid = loginResult.data.id

      // 2获取角色菜单树
      let userMenuTreeResult = await getUserMenuTreeApi(uid)
      userMenus.value = userMenuTreeResult.data

      // 3.获取用户信息
      const userInfoResult = await getUserInfoApi(uid)
      userinfo.value = userInfoResult.data

      

      // 4.1.存储用户信息
      localCache.setCache('userinfo', userinfo.value)
      // 4.2存储用户菜单列表户信息
      localCache.setCache('userMenus', userMenus.value)

      // 5动态添加路由
      const routes: RouteRecordRaw[] = mapMenusToRoutes3(userMenus.value,localRouter) // 将菜单映射成路由
      let MainContainer = asyncRoutes.find( item => item.path == '/' && item.name == "main")
      if (MainContainer){
          let children :any[] = MainContainer.children;
          children.push(...routes);
      }
      mapMenusToFirstPath(asyncRoutes) // 添加重定向路由 让每个菜单都能直接跳转到第一个子路由
      router.addRoute(asyncRoutes[0]);
      
      // 跳转到主页面
      router.push({name:'main'})
    } else {
      ElMessage.error(loginResult.msg)
    }
  }



  // 刷新页面时，重新加载所有静态信息，包括路由
function loadAllStaticInfo() {
    const neWtoken = localCache.getCache('token')
    const neWuserinfo = localCache.getCache('userinfo')
    const neWuserMenus = localCache.getCache('userMenus')
    if (neWtoken && neWuserinfo && neWuserMenus) {
      
      token.value = neWtoken
      userinfo.value = neWuserinfo
      userMenus.value = neWuserMenus
      
      // 刷新后 再次动态添加路由
      // const localRouter = loadLocalRouter() // 获取本地路由
      const routes: RouteRecordRaw[] = mapMenusToRoutes3(userMenus.value, localRouter) // 将菜单映射成路由
      let MainContainer = asyncRoutes.find( item => item.path == '/' && item.name == "main")
      if (MainContainer){
          let children :any[] = MainContainer.children;
          children.push(...routes);
          
      }
      mapMenusToFirstPath(asyncRoutes)  // 添加重定向路由 让每个菜单都能直接跳转到第一个子路由

      router.addRoute(asyncRoutes[0]);
      //routes.forEach(route => router.addRoute("main",route)) // 添加到路由中

    }
  }
  return { token, userinfo, userMenus, loginAccountAction ,loadAllStaticInfo }
})
